/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wampuswindow;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;

/**
 *
 * @author Jedrek
 */
public class MyCanvas extends Canvas{

    public static final int HIDDEN = 1;
    public static final int PID = 2;
    public static final int WIND = 4;
    public static final int SULFUR = 8;
    public static final int WAMPUS = 16;
    public static final int GOLD = 32;
    public static final int ENTERENCE_LEFT = 64;
    public static final int ENTERENCE_RIGHT = 128;
    public static final int ENTERENCE_UP = 256;
    public static final int ENTERENCE_DOWN = 512;

    int[][] map;

    Image imgBlueDragon;
    Image imgGreenDragon;
    Image imgBronzeDragon;
    Image imgPit;
    Image imgSulfur;
    Image imgWind;
    Image imgGold;
    Image imgUnknown;

    public MyCanvas(){
        super();

        map = null;

        //loading images
        URL url = WampusWindow.class.getResource("wampus/blue-dragon.jpg");
        imgBlueDragon = Toolkit.getDefaultToolkit().getImage(url);
        url = WampusWindow.class.getResource("wampus/Green-dragon.jpg");
        imgGreenDragon = Toolkit.getDefaultToolkit().getImage(url);
        url = WampusWindow.class.getResource("wampus/bronze-dragon.jpg");
        imgBronzeDragon = Toolkit.getDefaultToolkit().getImage(url);
        url = WampusWindow.class.getResource("wampus/pit.jpg");
        imgPit = Toolkit.getDefaultToolkit().getImage(url);
        url = WampusWindow.class.getResource("wampus/sulfur.jpg");
        imgSulfur = Toolkit.getDefaultToolkit().getImage(url);
        url = WampusWindow.class.getResource("wampus/wind.jpg");
        imgWind = Toolkit.getDefaultToolkit().getImage(url);
        url = WampusWindow.class.getResource("wampus/goldbars.jpg");
        imgGold = Toolkit.getDefaultToolkit().getImage(url);
        url = WampusWindow.class.getResource("wampus/unknown.jpg");
        imgUnknown = Toolkit.getDefaultToolkit().getImage(url);
    }

    @Override
    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        int width = getWidth();
        int height = getHeight();

        g.setColor(Color.white);
        g.fillRect(0, 0, width, height);

        g2.setColor(Color.black);

        if( map == null ){
            g2.drawString("NO MAP", getWidth()/2-20, getHeight()/2);
            return;
        }

        g2.drawRect(0, 0, width-1, height-1);
                
        for( int i = 0; i<map.length; i++ ){
            g2.drawLine(0, i*height/map.length, width, i*height/map.length);
        }
        
        for( int i = 0; i<map[0].length; i++ ){
            g2.drawLine(i*width/map[0].length, 0, i*width/map[0].length, height);
        }

        for( int i = 0; i<map.length; i++ )
            for( int j = 0; j<map[0].length; j++){
                String content = "";
                if( ( map[i][j] & HIDDEN ) != 0 ){
                    content+= "h,";
                    g2.drawImage(imgUnknown, j*width/map[0].length+1, i*height/map.length+1, width/map[0].length-2, height/map.length-2, this);
                }
                if( ( map[i][j] & PID ) != 0 ){
                    content+= "p,";
                    g2.drawImage(imgPit, j*width/map[0].length+1, i*height/map.length+1, width/map[0].length-2, height/map.length-2, this);
                }
                if( ( map[i][j] & WIND ) != 0 ){
                    content+= "w,";
                    g2.drawImage(imgWind, j*width/map[0].length+1, i*height/map.length+1, width/map[0].length-2, height/map.length-2, this);
                }
                if( ( map[i][j] & SULFUR ) != 0 ){
                    content+= "s,";
                    g2.drawImage(imgSulfur, j*width/map[0].length+1, i*height/map.length+1, width/map[0].length-2, height/map.length-2, this);
                }
                content += "\r\n";
                if( ( map[i][j] & WAMPUS ) != 0 ){
                    content+= "W,";
                    g2.drawImage(imgBronzeDragon, j*width/map[0].length+1, i*height/map.length+1, width/map[0].length-2, height/map.length-2, this);
                }
                if( ( map[i][j] & GOLD ) != 0 ){
                    content+= "g,";
                    g2.drawImage(imgGold, j*width/map[0].length+1, i*height/map.length+1, width/map[0].length-2, height/map.length-2, this);
                }
                content += "\r\n";
                if( ( map[i][j] & ENTERENCE_LEFT ) != 0 ){
                    content+= "l,";
                }
                if( ( map[i][j] & ENTERENCE_RIGHT ) != 0 )
                    content+= "r,";
                if( ( map[i][j] & ENTERENCE_UP ) != 0 )
                    content+= "u,";
                if( ( map[i][j] & ENTERENCE_DOWN ) != 0 )
                    content+= "d,";

                g2.drawString(content, j*width/map[0].length, i*height/map.length + 10);
            }
    }

    @Override
    public void update(Graphics g){
        paint(g);
    }

    public boolean setMap(int[][] map){
        if( map.length <= 0 )
            return false;
        
        for( int i = 0; i<map.length; i++ )
            if( map[i].length != map.length )
                return false;
        
        this.map = map;
        return true;
    }

    public void resetMap(){
        map = null;
    }
}
